<template>
  <el-dialog
    :title="!dataForm.brokerId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="经纪人用户名" prop="brokerUserName">
      <el-input v-model="dataForm.brokerUserName" placeholder="经纪人用户名"></el-input>
    </el-form-item>
    <el-form-item label="用户人真实姓名" prop="brokerName">
      <el-input v-model="dataForm.brokerName" placeholder="用户人真实姓名"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input v-model="dataForm.password" placeholder="密码"></el-input>
    </el-form-item>
    <el-form-item label="手机号" prop="phone">
      <el-input v-model="dataForm.phone" placeholder="手机号"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="修改时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="修改时间"></el-input>
    </el-form-item>
    <el-form-item label="是否删除 0否 1是 逻辑删除" prop="delStatus">
      <el-input v-model="dataForm.delStatus" placeholder="是否删除 0否 1是 逻辑删除"></el-input>
    </el-form-item>
    <el-form-item label="性别" prop="sex">
      <el-input v-model="dataForm.sex" placeholder="性别"></el-input>
    </el-form-item>
    <el-form-item label="公司id" prop="companyId">
      <el-input v-model="dataForm.companyId" placeholder="公司id"></el-input>
    </el-form-item>
    <el-form-item label="详细地址" prop="brokerAttrs">
      <el-input v-model="dataForm.brokerAttrs" placeholder="详细地址"></el-input>
    </el-form-item>
    <el-form-item label="所在区域" prop="cityId">
      <el-input v-model="dataForm.cityId" placeholder="所在区域"></el-input>
    </el-form-item>
    <el-form-item label="是否在职 1在职 0离职" prop="brokerJob">
      <el-input v-model="dataForm.brokerJob" placeholder="是否在职 1在职 0离职"></el-input>
    </el-form-item>
    <el-form-item label="手机号状态 1正在使用  0 不使用" prop="phoneStatus">
      <el-input v-model="dataForm.phoneStatus" placeholder="手机号状态 1正在使用  0 不使用"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          brokerId: 0,
          brokerUserName: '',
          brokerName: '',
          password: '',
          phone: '',
          createTime: '',
          updateTime: '',
          delStatus: '',
          sex: '',
          companyId: '',
          brokerAttrs: '',
          cityId: '',
          brokerJob: '',
          phoneStatus: ''
        },
        dataRule: {
          brokerUserName: [
            { required: true, message: '经纪人用户名不能为空', trigger: 'blur' }
          ],
          brokerName: [
            { required: true, message: '用户人真实姓名不能为空', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '密码不能为空', trigger: 'blur' }
          ],
          phone: [
            { required: true, message: '手机号不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ],
          delStatus: [
            { required: true, message: '是否删除 0否 1是 逻辑删除不能为空', trigger: 'blur' }
          ],
          sex: [
            { required: true, message: '性别不能为空', trigger: 'blur' }
          ],
          companyId: [
            { required: true, message: '公司id不能为空', trigger: 'blur' }
          ],
          brokerAttrs: [
            { required: true, message: '详细地址不能为空', trigger: 'blur' }
          ],
          cityId: [
            { required: true, message: '所在区域不能为空', trigger: 'blur' }
          ],
          brokerJob: [
            { required: true, message: '是否在职 1在职 0离职不能为空', trigger: 'blur' }
          ],
          phoneStatus: [
            { required: true, message: '手机号状态 1正在使用  0 不使用不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.brokerId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.brokerId) {
            this.$http({
              url: this.$http.adornUrl(`/broker/brokerbroker/info/${this.dataForm.brokerId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.brokerUserName = data.brokerBroker.brokerUserName
                this.dataForm.brokerName = data.brokerBroker.brokerName
                this.dataForm.password = data.brokerBroker.password
                this.dataForm.phone = data.brokerBroker.phone
                this.dataForm.createTime = data.brokerBroker.createTime
                this.dataForm.updateTime = data.brokerBroker.updateTime
                this.dataForm.delStatus = data.brokerBroker.delStatus
                this.dataForm.sex = data.brokerBroker.sex
                this.dataForm.companyId = data.brokerBroker.companyId
                this.dataForm.brokerAttrs = data.brokerBroker.brokerAttrs
                this.dataForm.cityId = data.brokerBroker.cityId
                this.dataForm.brokerJob = data.brokerBroker.brokerJob
                this.dataForm.phoneStatus = data.brokerBroker.phoneStatus
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/broker/brokerbroker/${!this.dataForm.brokerId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'brokerId': this.dataForm.brokerId || undefined,
                'brokerUserName': this.dataForm.brokerUserName,
                'brokerName': this.dataForm.brokerName,
                'password': this.dataForm.password,
                'phone': this.dataForm.phone,
                'createTime': this.dataForm.createTime,
                'updateTime': this.dataForm.updateTime,
                'delStatus': this.dataForm.delStatus,
                'sex': this.dataForm.sex,
                'companyId': this.dataForm.companyId,
                'brokerAttrs': this.dataForm.brokerAttrs,
                'cityId': this.dataForm.cityId,
                'brokerJob': this.dataForm.brokerJob,
                'phoneStatus': this.dataForm.phoneStatus
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
